using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Pervote.Core;
using Pervote.Core.Logic;

public partial class Controls_PollList : System.Web.UI.UserControl
{
    private const int ItemsPerPage = 20;
    private const int HotPollsPeriod = 48; //2 days

    string category = "";
    int pageNumber = 0;
    string listType = "";

    private int PageNumber
    {
        get { return pageNumber; }
        set { pageNumber = value; if (pageNumber < 0) pageNumber = 0; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        category = Request["Category"];
        if (String.IsNullOrEmpty(category)) category = "/";
        
        string strPageNumber = Request["Page"];
        if (!String.IsNullOrEmpty(strPageNumber)) PageNumber = int.Parse(strPageNumber);
        else PageNumber = 0;

        listType = Request["ListType"];
        if (String.IsNullOrEmpty(listType)) listType = "HotPolls";

        string action = Request["Action"];
        if (action == "Next")
        {
            PageNumber++;
        }
        if (action == "Back")
        {
            PageNumber--;
        }

        LoadListType();
    }

    protected void LoadHotPolls()
    {
        List<Poll> polls = new PollManager().GetHotPollsList(category, (pageNumber * ItemsPerPage) + 1, ItemsPerPage, HotPollsPeriod);
        if (polls.Count == 0) pageNumber--;
        pollListRepeater.DataSource = polls;
        pollListRepeater.DataBind();
    }

    protected void LoadTopPolls()
    {
        List<Poll> polls = new PollManager().GetTopPollsList(category, (pageNumber * ItemsPerPage) + 1, ItemsPerPage);
        if (polls.Count == 0) pageNumber--;
        pollListRepeater.DataSource = polls;
        pollListRepeater.DataBind();
    }

    protected void LoadRandomPolls()
    {
        List<Poll> polls = new PollManager().GetRandomPollsList(category, ItemsPerPage);
        if (polls.Count == 0) pageNumber--;
        pollListRepeater.DataSource = polls;
        pollListRepeater.DataBind();
    }

    protected void LoadNewPolls()
    {
        List<Poll> polls = new PollManager().GetNewPollsList(category, (pageNumber * ItemsPerPage) + 1, ItemsPerPage);
        if (polls.Count == 0) pageNumber--;
        pollListRepeater.DataSource = polls;
        pollListRepeater.DataBind();
    }

    private void LoadListType()
    {
        switch (listType)
        {
            case "HotPolls":
                LoadHotPolls();
                break;
            case "NewPolls":
                LoadNewPolls();
                break;
            case "TopPolls":
                LoadTopPolls();
                break;
            case "RandomPolls":
                LoadRandomPolls();
                break;
        }
    }

    protected string GetBackHref()
    {
        return "/PollListMain.aspx?Category=" + category + "&Page=" + pageNumber + "&Action=Back&ListType=" + listType;
    }

    protected string GetNextHref()
    {
        return "/PollListMain.aspx?Category=" + category + "&Page=" + pageNumber + "&Action=Next&ListType=" + listType;
    }

    protected string GetListTypeLinkDecoration(string listType)
    {
        if (listType == this.listType) return "green";
        else return "blue";
    }

    protected string GetListLink(string listType)
    {
        return "/PollListMain.aspx?Category=" + category + "&ListType=" + listType;
    }

    protected string GetCategoryPathLink()
    {
        string[] categories = category.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);

        string linkText = "";
        for (int i = 0; i < categories.Length; i++)
        {
            linkText += " > <a href=\"CategoryMain.aspx?name=/" + string.Join("/", categories, 0, i + 1) + "/\">" + categories[i] + "</a> ";
        }

        return linkText;
    }

    protected string GetCurrentSelection()
    {
        switch(this.listType)
        {
            case "HotPolls":
                return " > Hot Polls";
            case "NewPolls":
                return " > New Polls";
            case "TopPolls":
                return " > Most Voted";
            case "RandomPolls":
                return " > Random Polls";
            default:
                return "";
        }
    }
}
